/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJDialog.java
 *
 * Created on 13.5.2010, 15:02:37
 */
package pv1682010.gui;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
import javax.swing.JOptionPane;
import pv1682010.Event;

/**
 *
 * @author Petr
 */
public class EventDialog extends javax.swing.JDialog {

    private int courseNumber = 0;
    private ResourceBundle translations;

    /** Creates new form NewJDialog */
    public EventDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        translations = Translations.getRb();
        initComponents();
    }

    public int getCourse() {
        return courseNumber;
    }

    public void setCourseNumber(int courseNumber) {
        this.courseNumber = courseNumber;
        ((EventTableModel) eventTable.getModel()).setCourse(courseNumber);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        eventAddDialog = new javax.swing.JDialog();
        eventCancelButton = new javax.swing.JButton();
        eventOkButton = new javax.swing.JButton();
        nameNameField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        startNameField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        endNameField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        eventTable = new javax.swing.JTable();
        deleteEventsButton = new javax.swing.JButton();
        addEventsButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        eventAddDialog.setModal(true);

        eventCancelButton.setText(translations.getString("cancel"));
        eventCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventCancelButtonActionPerformed(evt);
            }
        });

        eventOkButton.setText("OK");
        eventOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventOkButtonActionPerformed(evt);
            }
        });

        jLabel1.setText(translations.getString("event_name"));

        jLabel2.setText(translations.getString("event_start"));

        startNameField.setText("MM/DD/YYYY HH:MM");
        startNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startNameFieldActionPerformed(evt);
            }
        });

        jLabel4.setText(translations.getString("event_end"));

        endNameField.setText("MM/DD/YYYY HH:MM");

        javax.swing.GroupLayout eventAddDialogLayout = new javax.swing.GroupLayout(eventAddDialog.getContentPane());
        eventAddDialog.getContentPane().setLayout(eventAddDialogLayout);
        eventAddDialogLayout.setHorizontalGroup(
            eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eventAddDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, eventAddDialogLayout.createSequentialGroup()
                        .addGroup(eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(startNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(endNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nameNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(eventAddDialogLayout.createSequentialGroup()
                        .addComponent(eventOkButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eventCancelButton)))
                .addContainerGap(176, Short.MAX_VALUE))
        );
        eventAddDialogLayout.setVerticalGroup(
            eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eventAddDialogLayout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(startNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(endNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(eventAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(eventOkButton)
                    .addComponent(eventCancelButton))
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        eventTable.setModel(new EventTableModel());
        jScrollPane1.setViewportView(eventTable);

        deleteEventsButton.setText(translations.getString("event_delete"));
        deleteEventsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteEventsButtonActionPerformed(evt);
            }
        });

        addEventsButton.setText(translations.getString("event_add_title"));
        addEventsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addEventsButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addEventsButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteEventsButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 281, Short.MAX_VALUE)
                .addComponent(okButton)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addEventsButton)
                    .addComponent(deleteEventsButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void addEventsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addEventsButtonActionPerformed
        eventAddDialog.pack();
        eventAddDialog.setVisible(true);
}//GEN-LAST:event_addEventsButtonActionPerformed

    private void deleteEventsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteEventsButtonActionPerformed
        EventTableModel eventModel = (EventTableModel) eventTable.getModel();
        Set<Event> events = new HashSet<Event>();
        for (int i : eventTable.getSelectedRows()) {
            Event event = new Event();
            event.setEventNumber(Integer.parseInt(eventTable.getValueAt(i, 0).toString()));
            event.setEventName(eventTable.getValueAt(i, 1).toString());
            events.add(event);
        }
        for (Event event : events) {
            eventModel.deleteEvent(event);
        }
}//GEN-LAST:event_deleteEventsButtonActionPerformed

    private void eventOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventOkButtonActionPerformed
        try {
            EventTableModel eventModel = (EventTableModel) eventTable.getModel();
            Event event = new Event();

            if ("".equals(startNameField.getText())) {
                throw new IllegalArgumentException(translations.getString("fill_start"));
            }

            if ("".equals(endNameField.getText())) {
                throw new IllegalArgumentException(translations.getString("fill_end"));
            }
            event.setEventName(nameNameField.getText());
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
            Date start = df.parse(startNameField.getText());
            Date end = df.parse(endNameField.getText());
            event.setStartDate(start);
            event.setEndDate(end);

            eventModel.addEvent(event);
            resetEvents();
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
}//GEN-LAST:event_eventOkButtonActionPerformed

    private void eventCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventCancelButtonActionPerformed
        resetEvents();
}//GEN-LAST:event_eventCancelButtonActionPerformed

    private void startNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startNameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_startNameFieldActionPerformed

    private void resetEvents() {
        nameNameField.setText("");
        startNameField.setText("");
        endNameField.setText("");
        eventAddDialog.setVisible(false);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addEventsButton;
    private javax.swing.JButton deleteEventsButton;
    private javax.swing.JTextField endNameField;
    private javax.swing.JDialog eventAddDialog;
    private javax.swing.JButton eventCancelButton;
    private javax.swing.JButton eventOkButton;
    private javax.swing.JTable eventTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nameNameField;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField startNameField;
    // End of variables declaration//GEN-END:variables
}
